package com.dy.design_pattern_study.prototype_mode.dome1.impl;

import com.dy.design_pattern_study.prototype_mode.dome1.Prototype;
import lombok.Data;
import lombok.ToString;

/**
 * @CreateBy zhangdingyi
 * @Date 2023-03-22
 * @Content 原型模式
 * 原型模式（Prototype），用原型实例指定创建对象的种类，并且通过拷贝这些原型创建新的对象。
 * 这个clone是基于jvm相当于直接生成一个对象在 硬盘中
 */
@Data
//ToString 注解重写打印类
@ToString(callSuper = true)
public class ConcretePrototype1 extends Prototype {

    private String name;

    @Override
    public Prototype Clone() throws CloneNotSupportedException {
        return (ConcretePrototype1) this.clone();
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        ConcretePrototype1 one = new ConcretePrototype1();
        one.setId("1");
        System.out.println("one address= " + Integer.toHexString(System.identityHashCode(one)));

        ConcretePrototype1 prototype = (ConcretePrototype1) one.Clone();
        prototype.setId("2");
        prototype.setName("prototype");
        System.out.println("prototype address= " + Integer.toHexString(System.identityHashCode(prototype)));

        System.out.println("one = " + one);
        System.out.println("prototype = " + prototype.toString());
        //System.out.println("prototype = " + prototype.getId() + prototype.getName());

        //看看这两个对象地址是否相同 --结果是false 说明这两个对象是独立的对象，这意味着这个克隆是深克隆
        boolean equals = one.equals(prototype);
        System.out.println("equals = " + equals);



    }
}
